{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6c696e7f",
   "metadata": {},
   "source": [
    "* 记录人：曾烨\n",
    "* week14"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "55513d94",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 本周内容\n",
    "## 函数的定义\n",
    "## 如何使用函数\n",
    "## 函数的返回值\n",
    "## 使用注释改进文档\n",
    "## 传递任意数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8cb338f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数的定义\n",
    "* 定义函数\n",
    "### def定义函数的第一步\n",
    "### 写定义函数的名称： def function_name\n",
    "### function_name 遵循变量名称定义的规则\n",
    "### 函数名称后加上\"():\" def function_name(): ===> f(x) = 2x\n",
    "\n",
    "* 书写函数体\n",
    "### 函数的注释\n",
    "### 写函数的具体操作\n",
    "### 注意：函数也需要tab键的间距\n",
    "\n",
    "* 使用函数\n",
    "### function_name()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "1c880666",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fib(n):    # write Fibonacci series up to n\n",
    "...     \"\"\"Print a Fibonacci series up to n.\"\"\"\n",
    "...     a, b = 0, 1\n",
    "...     while a < n:\n",
    "...         print(a, end=' ')\n",
    "...         a, b = b, a+b\n",
    "...     print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "e332a76c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 \n"
     ]
    }
   ],
   "source": [
    "fib(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2dc1e731",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 13 21 34 55 89 \n"
     ]
    }
   ],
   "source": [
    "fib(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "f788f9dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "def hello():\n",
    "    \"\"\"打印hello world\"\"\"\n",
    "    print(\"hello world\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "7c8e2aef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "81d64749",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:eee\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'e'}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = vowels.intersection(set(word))\n",
    "found\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "dc1cb139",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search(word):\n",
    "    \"\"\"打印文本中出现的元音字母\n",
    "    请输入你想要查询的文本内容\n",
    "    \"\"\"\n",
    "    vowels = set('aeiou')\n",
    "    word = input(\"Provide a word to search for vowels:\")\n",
    "    found = vowels.intersection(set(word)) # 交集运算\n",
    "    print(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "10b9efcd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function search in module __main__:\n",
      "\n",
      "search(word)\n",
      "    打印文本中出现的元音字母\n",
      "    请输入你想要查询的文本内容\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(search)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3e6ced5f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30a0e79a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数存在的意义\n",
    "## 可以通过调用函数的方式实现某些功能，使用人不需要考虑函数代码内容\n",
    "## 节省时间，提升应用的效率\n",
    "## 函数一旦定义，即可重复使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "329c0312",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "674e55e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数的参数\n",
    "## 形式参数\n",
    "### 在定义过程中，函数的参数为形式参数\n",
    "### 形式函数是虚拟的，不占用空间的\n",
    "## 实际参数\n",
    "### 调用函数过程中，函数的参数为实际参数\n",
    "### 调用函数时，形式参数被分配空间，成为实际参数并应用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "5f8e6d66",
   "metadata": {},
   "outputs": [],
   "source": [
    "def greet_user(username):\n",
    "    \"\"\"显示简单的问候语\"\"\"\n",
    "    print(\"hello,\"+username.title()+\"!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "b06d1294",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello,Zhichao!\n"
     ]
    }
   ],
   "source": [
    "greet_user(\"zhichao\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "4e0556f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def describe_pet(animal_type,pet_name):\n",
    "    \"\"\"显示宠物信息\"\"\"\n",
    "    print(\"\\nI have a\" + animal_type + \".\")\n",
    "    print(\"My\" + animal_type + \"'s name is \"+pet_name.title() + \".\")\n",
    " \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "974ecd2c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have adog.\n",
      "Mydog's name is 豆本豆.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(\"dog\",\"豆本豆\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a1b8ac3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 当参数不止一个时：\n",
    "## 位置参数有多个值，如果实际参数直接赋值，要按照形式参数的先后顺序，这种方法称：位置参数赋值\n",
    "### 调用函数过程中想要考虑如下：考虑位置 考虑个数\n",
    "\n",
    "\n",
    "## 关键字参数\n",
    "### 在调用函数时使用，用赋值的过程中去定义形参\n",
    "### 不需要考虑顺序，因为赋值已经确定了形式参数的值\n",
    "## 默认参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "d01f10f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 关键字参数\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a5c893f1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "24790712",
   "metadata": {},
   "source": [
    "* 实操和注意\n",
    "### 位置参数需要放在关键字参数的前面"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "dc442b24",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "func_test() missing 1 required positional argument: 'y'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-49-727ef830f700>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      4\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mfunc_test\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: func_test() missing 1 required positional argument: 'y'"
     ]
    }
   ],
   "source": [
    "#eg1\n",
    "def func_test(x,y):\n",
    "    print(x)\n",
    "    print(y)\n",
    "    \n",
    "func_test(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c4b08b5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64a54586",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
